/********************************************************************
	file base:	EmbedDlg
	file ext:	h
	author:		JiangHuai
	
	purpose:	Embed Dlg
*********************************************************************/

#ifndef EMBED_DLG
#define EMBED_DLG

#include "resource.h"
#include "GlobalVariabel.h"
#include "Lib/CXImage600/include/ximage.h"
#include "EmbedAndExtract.h"
#include "Info.h"
#include "Utility.h"

INT CALLBACK EmbedDialogProc(      
								   HWND hwndDlg,
								   UINT uMsg,
								   WPARAM wParam,
								   LPARAM lParam
								   ) {
	switch (uMsg)
	{
	case WM_INITDIALOG:
		EnableWindow(GetDlgItem(hwndDlg,IDC_SAVE), false);
		break;
	//control command
	case WM_COMMAND:

		switch (LOWORD(wParam))
		{
		case IDC_SAVE:
			ofn.hwndOwner = hwndDlg;
			if (GetSaveFileName(&ofn))
			{
				info.embed.Save(OpenFileName, getFormat(OpenFileName));
			}
			break;
		case IDC_IMAGE_BROWSER:
			ofn.hwndOwner = hwndDlg;
			if (GetOpenFileName(&ofn))
			{
				SetDlgItemText(hwndDlg, IDC_FILEPATH, (LPCTSTR)OpenFileName);
			}
			break;
		case IDCANCEL:
			EndDialog(hwndDlg, 0);
			break;
		case IDC_FILEPATH:
			GetDlgItemText(hwndDlg, IDC_FILEPATH, (LPSTR)&OpenFileName, MAX_LENGTH);
			break;
		case ID_EMBED:
			info.fileType = getFormat(OpenFileName);
			info.image.Load((const TCHAR*)&OpenFileName, info.fileType);

			int compress = IsDlgButtonChecked(hwndDlg, IDC_COMPRESS);
			if (compress == BST_CHECKED) {
				info.PayLoad.compressed = true;
			} else info.PayLoad.compressed = false;
			
			WCHAR key[MAX_LENGTH];
			int status = GetDlgItemText(hwndDlg, IDC_KEY, (LPSTR)&key, MAX_LENGTH);
			if (status != 0) {
				info.key = string((char*)key);
				info.PayLoad.encrypted = true;
			} else info.PayLoad.encrypted = false;


			WCHAR plainText[MAX_LENGTH];
			status = GetDlgItemText(hwndDlg,IDC_EMBED_PLAIN_TEXT, (LPSTR)&plainText, MAX_LENGTH);
			if (status != 0 || status < MAX_LENGTH) {
				info.plainText = string((char *)plainText);
			}

			status = Embed();
			if (status == 1) {
				MessageBox(hwndDlg, (LPCSTR)"Plain Text is too long", (LPCSTR)"Warnning", MB_ICONWARNING | MB_OK);
			}
			info.imageAvaliable = true;
			info.embedAvaliable = true;
			EnableWindow(GetDlgItem(hwndDlg,IDC_SAVE), true);
			InvalidateRect(GetParent(hwndDlg), NULL, true);
			break;
		}
		break;
	}
	return 0;
}

#endif